﻿using DFGK.IService;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Dto;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;

namespace DFGK_WMS.WebAPI.Controllers.EBS
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class EBSSystemController : BaseController
    {
        private readonly IMaterialService materialService;
        private readonly IReceivingBillService receivingBillService;
        private readonly IShippingOrderDetailService shippingOrderDetailService;

        public EBSSystemController(IWebHostEnvironment environment, IMaterialService materialService, IReceivingBillService receivingBillService, IShippingOrderDetailService shippingOrderDetailService) : base(environment)
        {
            this.materialService = materialService;
            this.receivingBillService = receivingBillService;
            this.shippingOrderDetailService = shippingOrderDetailService;
        }

        /// <summary>
        /// EBS系统同步物料数据
        /// </summary>
        /// <param name="eBSMaterialDatas"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult EBSAddListMaterial(List<EBSMaterialDataDTO> eBSMaterialDatas)
        {
            var IsOk = materialService.EBSAddMaterialList(eBSMaterialDatas);
            if (IsOk)
            {
                return Ok(BaseResult.Result(true, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败！编码已存在！", "500"));
        }

        /// <summary>
        /// EBS下发入库单据(收货单)
        /// </summary>
        /// <param name="eBSReceivingBill"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult EBSAddReceivingBill(EBSReceivingBillDTO eBSReceivingBill)
        {
            var IsOk = receivingBillService.EBSAddReceivingBill(eBSReceivingBill, out string message);
            if (IsOk)
            {
                return Ok(BaseResult.Result(true, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败！" + message, "500"));
        }

        /// <summary>
        /// EBS下发出库单据
        /// </summary>
        /// <param name="eBSShippingOrder"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult EBSAddShippingOrder(EBSShippingOrderDTO eBSShippingOrder)
        {
            var IsOk = shippingOrderDetailService.EBSAddShippingOrderDetail(eBSShippingOrder, out string message);
            if (IsOk)
            {
                return Ok(BaseResult.Result(true, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败！" + message, "500"));
        }
        /// <summary>
        /// EBS下发出入库单据
        /// </summary>
        /// <param name="eBSDocuments"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult EBSIssueInOutOrder(EBSDocumentsDTO eBSDocuments)
        {
            if (string.IsNullOrEmpty(eBSDocuments.OrderSn) || (eBSDocuments.OrderType <= 0 && eBSDocuments.OrderType > 2) || eBSDocuments.items.Count() <= 0)
            {
                return Ok(BaseResult.Result( "400","参数有误！"));
            }
            bool result = false;
            string message = string.Empty;
            if (eBSDocuments.OrderType == 1)
            {
                EBSReceivingBillDTO eBSReceivingBill = new();
                eBSReceivingBill.BilNo = eBSDocuments.OrderSn;
                eBSReceivingBill.BillType = eBSDocuments.BillType;
                List<EBSReceivingBillDetailListDTO> items = new();
                foreach (var item in eBSDocuments.items)
                {
                    items.Add(new EBSReceivingBillDetailListDTO()
                    {
                        MaterialCode = item.MaterialCode,
                        MaterialName = item.MaterialName,
                        Qty = item.Qty
                    });
                }
                eBSReceivingBill.items = items;
                result = receivingBillService.EBSAddReceivingBill(eBSReceivingBill, out message);
            }
            else
            {
                EBSShippingOrderDTO eBSShippingOrder = new();
                eBSShippingOrder.OrderSn = eBSDocuments.OrderSn;
                eBSShippingOrder.BillType = eBSDocuments.BillType;
                List<EBSShippingOrderDetailListDTO> items = new();
                foreach (var item in eBSDocuments.items)
                {
                    items.Add(new EBSShippingOrderDetailListDTO()
                    {
                        MaterialCode = item.MaterialCode,
                        MaterialName = item.MaterialName,
                        OutQty = item.Qty
                    });
                }
                eBSShippingOrder.items = items;
                result = shippingOrderDetailService.EBSAddShippingOrderDetail(eBSShippingOrder, out message);
            }
            if (result)
            {
                return Ok(BaseResult.Result(true, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败！" + message, "500"));
        }
    }
}
